package com.cy.dbsys.controller;

import com.cy.commom.pojo.JsonResult;
import com.cy.dbsys.pojo.SysUser;
import com.cy.dbsys.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class SysUserController {
    @Autowired
    SysUserService sysUserService;

    /**修改密码*/
    @RequestMapping("/doUpdatePassword")
    public JsonResult doUpdatePassword(String pwd,String newPwd,String cfgPwd){
        sysUserService.updatePassword(pwd,newPwd,cfgPwd);
        return new JsonResult("update ok");
    }

    /**(认证模块)*/
    @RequestMapping("/doLogin")
    public JsonResult doLogin(String username,String password,boolean isRememberMe){
        //1.获取Subject对象
        Subject subject= SecurityUtils.getSubject();
        //2.通过Subject提交用户信息,交给shiro框架进行认证操作
        //2.1对用户进行封装
        UsernamePasswordToken token=
                new UsernamePasswordToken(
                        username,//身份信息
                        password);//凭证信息
        if(isRememberMe) token.setRememberMe(true);
        //2.2对用户信息进行身份认证
        subject.login(token);
        //分析:
        //1)token会传给shiro的SecurityManager
        //2)SecurityManager将token传递给认证管理器
        //3)认证管理器会将token传递给realm
        return new JsonResult("login ok");
    }

    /**(修改模块)2、修改信息和用户与角色的关系数据*/
    @RequestMapping("/doUpdateObject")
    public JsonResult doUpdateObject(SysUser entity,Integer[] roleIds){
        sysUserService.updateObject(entity,roleIds);
        return new JsonResult("update ok");
    }
    /**(修改模块)1、根据id查询用户相关信息*/
    @RequestMapping("/doFindObjectById")
    public JsonResult doFindObjectById(Integer id){
        return new JsonResult(sysUserService.findObjectById(id));
    }

    /**(添加模块)*/
    @RequestMapping("/doSaveObject")
    public JsonResult doSaveObject(SysUser entity, Integer[] roleIds){
        sysUserService.saveObject(entity,roleIds);
        return new JsonResult("save ok");
    }

    /**禁用启用*/
    @RequestMapping("/doValidById")
    public JsonResult doValidById(Integer id,Integer valid){
        sysUserService.validById(id,valid);
        return new JsonResult("update ok");
    }

    /**(查询模块)根据id查询信息*/
    @RequestMapping("/doFindPageObjects")
    public JsonResult doFindPageObjects(String username,Integer pageCurrent) {
        return new JsonResult(sysUserService.findPageObjects(username, pageCurrent));
    }

}
